package com.ra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ra.exception.BizException;
import com.ra.mapper.PhotoMapper;
import com.ra.pojo.Photo;
import com.ra.pojo.PhotoAlbum;
import com.ra.mapper.PhotoAlbumMapper;
import com.ra.pojo.RespPageBean;
import com.ra.pojo.dto.ArticleAdminDTO;
import com.ra.pojo.dto.ConditionDTO;
import com.ra.pojo.dto.PhotoAlbumAdminDTO;
import com.ra.pojo.dto.PhotoAlbumDTO;
import com.ra.service.IPhotoAlbumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ra.utils.BeanCopyUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.ra.constant.CommonConstant.*;

/**
 * <p>
 * 相册 服务实现类
 * </p>
 *
 * @author 今晚几点睡_Ada
 * @since 2023-03-09
 */
@Service
public class PhotoAlbumServiceImpl extends ServiceImpl<PhotoAlbumMapper, PhotoAlbum> implements IPhotoAlbumService {

    @Autowired
    private PhotoAlbumMapper photoAlbumMapper;

    @Autowired
    private PhotoMapper photoMapper;
    @Override
    public RespPageBean<PhotoAlbumAdminDTO> listPhotoAlbumsAdmin(ConditionDTO conditionDTO) {
        Integer selectCount = photoAlbumMapper.selectCount(new LambdaQueryWrapper<PhotoAlbum>()
                /* <pre>
                 * StringUtils.isNotBlank(null)      = false
                 * StringUtils.isNotBlank("")        = false
                 * StringUtils.isNotBlank(" ")       = false
                 * StringUtils.isNotBlank("bob")     = true
                 * StringUtils.isNotBlank("  bob  ") = true
                 * </pre>*/
                .like(StringUtils.isNotBlank(conditionDTO.getKeywords()), PhotoAlbum::getAlbumName, conditionDTO.getKeywords())
                .eq(PhotoAlbum::getIsDelete, FALSE));
        if (selectCount==ZERO){
            return new RespPageBean<>();
        }
        List<PhotoAlbumAdminDTO> page= photoAlbumMapper.listPhotoAlbumsAdmin(conditionDTO);
        page.forEach(System.out::println);
        return new RespPageBean<>(Long.parseLong(selectCount.toString()),page);
    }

    @Override
    @SneakyThrows
    @Transactional
    public void saveOrUpdatePhotoAlbum(PhotoAlbumDTO photoAlbumDto) {
        PhotoAlbum photoAlbum = photoAlbumMapper.selectOne(new LambdaQueryWrapper<PhotoAlbum>()
                .select(PhotoAlbum::getId)
                .eq(PhotoAlbum::getAlbumName, photoAlbumDto.getAlbumName()));
        //System.out.println("photoAlbum = " + photoAlbum);
        if (Objects.nonNull(photoAlbum)&& !photoAlbum.getId().equals(photoAlbumDto.getId())){
            throw new BizException("相册名称已存在");
        }
        PhotoAlbum album = BeanCopyUtil.copyObject(photoAlbumDto, PhotoAlbum.class);
        this.saveOrUpdate(album);
      //System.out.println("album = " + album);
    }

    @Override
    @Transactional
    public void deletePhotoAlbumById(Integer albumId) {
        //查看是否有照片
        Integer count = photoMapper.selectCount(new LambdaQueryWrapper<Photo>()
                .eq(Photo::getAlbumId, albumId));
        if (count > 0) {
            //逻辑删除 isdelete =1
            photoAlbumMapper.updateById(PhotoAlbum.builder()
                    .id(albumId)
                    .isDelete(TRUE)
                    .build());
            photoMapper.update(new Photo(), new LambdaUpdateWrapper<Photo>()
                    .set(Photo::getIsDelete, TRUE)
                    .eq(Photo::getAlbumId, albumId));
        } else {
            photoAlbumMapper.deleteById(albumId);
        }
    }

    //根据id获取后台相册信息
    @Override
    public PhotoAlbumAdminDTO getPhotoAlbumByIdAdmin(Integer albumId) {
        PhotoAlbum photoAlbum = photoAlbumMapper.selectById(albumId);
        Integer photoCount = photoMapper.selectCount(new LambdaQueryWrapper<Photo>()
                .eq(Photo::getAlbumId, albumId)
                .eq(Photo::getIsDelete, FALSE));

        System.out.println("photoAlbum = " + photoAlbum);
        PhotoAlbumAdminDTO album = BeanCopyUtil.copyObject(photoAlbum, PhotoAlbumAdminDTO.class);
        //0张  显示多少张照片
        album.setPhotoCount(photoCount);
        System.out.println("album = " + album);
        return album;
    }

    @Override
    public List<PhotoAlbumDTO> listPhotoAlbumInfosAdmin() {
        List<PhotoAlbum> photoAlbums = photoAlbumMapper.selectList(new LambdaQueryWrapper<PhotoAlbum>()
                .eq(PhotoAlbum::getIsDelete, FALSE));
        return BeanCopyUtil.copyList(photoAlbums, PhotoAlbumDTO.class);
    }

    @Override
    public List<PhotoAlbumDTO> listPhotoAlbums() {
        List<PhotoAlbum> photoAlbumList = photoAlbumMapper.selectList(new LambdaQueryWrapper<PhotoAlbum>()
                //1公开的
                .eq(PhotoAlbum::getStatus, ONE)
                .eq(PhotoAlbum::getIsDelete, FALSE)
                .orderByDesc(PhotoAlbum::getId));
        return BeanCopyUtil.copyList(photoAlbumList, PhotoAlbumDTO.class);
    }

}
